package com.xuecheng.content.controller;


import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.CourseBaseConverrt;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CoursePubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 * @since 2022-06-14
 */
@Slf4j
@RestController
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService courseBaseService;

    @Autowired
    private CoursePubService coursePubService;


    /**
     * SpringMVC默认请求的接受参数类型为：QueryString（问号传参）
     *      QueryString：@RequestParam（默认可以省略）
     *      Restful：@Pathvariable
     *      RequestBody：@Requestbody
     *
     *
     * @param params
     * @param model
     * @return
     */
    @PostMapping("course/list")
    public PageVO<CourseBaseDTO> queryCourseList(PageRequestParams params,
                                                 @RequestBody QueryCourseModel model) {

        //1.获得请求头中的信息并解析拿到公司的id
        Long companyId = SecurityUtil.getCompanyId();


        PageVO pageVO=courseBaseService.queryCourseList(params,model,companyId);
        return pageVO;
    }

    @PostMapping("course")
    public CourseBaseDTO createCourse(@RequestBody CourseBaseVO vo) {
        //1.获得请求头中的信息并解析拿到公司的id
        Long companyId = SecurityUtil.getCompanyId();

        //2.将vo转为dto
        CourseBaseDTO dto = CourseBaseConverrt.INSTANCE.vo2dto(vo);

        dto.setCompanyId(companyId);

        return courseBaseService.createCourse(dto);
    }

    @GetMapping("course/{courseBaseId}")
    public CourseBaseDTO getCourseById(Long courseBaseId) {

        Long companyId = SecurityUtil.getCompanyId();

        return courseBaseService.getCourseById(courseBaseId,companyId);
    }

    @PutMapping("course")
    public CourseBaseDTO modifyCourseById(@RequestBody CourseBaseVO vo) {

        CourseBaseDTO dto = CourseBaseConverrt.INSTANCE.vo2dto(vo);

        Long companyId = SecurityUtil.getCompanyId();

        dto.setCompanyId(companyId);

        return courseBaseService.modifyCourseById(dto);
    }

    @DeleteMapping("course/{courseId}")
    public void removeCourseBaseById(Long courseBaseId) {

        Long companyId = SecurityUtil.getCompanyId();

        courseBaseService.removeCourseBase(courseBaseId,companyId);
    }

    /**
     * 提交审核
     * @param courseBaseId
     */
    @GetMapping("course/commit/{courseBaseId}")
    public void commitCourseById(@PathVariable Long courseBaseId) {

        Long companyId = SecurityUtil.getCompanyId();

        courseBaseService.commitCourseById(courseBaseId,companyId);
    }

    /**
     * SpringMvc中结果解析器并存
     *      @RestController：SpringMVc对于方法所返回的内容都使用jackson解析器来输出--字符串、json字符串
     *      方法要返回ModelAndView对象：使用freemarker的结果视图器来解析内容
     *          model：view
     *      只要在RestController中添加下面的条件，就可以让freemarker结果视图器并存
     *          1.添加freemarker启动器
     *          2.添加freemarker功能配置
     *              freemarker。enable=true
     *          3.controller方法返回值要是ModelAndView
     * @param courseId
     * @param companyId
     * @return
     */
    @GetMapping("course/preview/{courseBaseId}/{companyId}")
    public Object previewCourse(@PathVariable Long courseId, @PathVariable Long companyId) {

        //调用service莫得数据模型
        Map<String, Object> dataMap = coursePubService.previewCourse(courseId,companyId);

        //2.构建数据模型和结果视图名称对象
        ModelAndView modelAndView = new ModelAndView("learning_article", dataMap);
        return modelAndView;
    }



    @PostMapping("course_pub/publish/{courseBaseId}")
    public void publishCourse(@PathVariable Long courseBaseId){

        Long companyId = SecurityUtil.getCompanyId();

        coursePubService.publishCourse(courseBaseId,companyId,false);
    }

}
